//2020年6月14日23:06:57
//========================================初始化开始=================================================//
//屏幕分辨率检测
Dim screenX = GetScreenX()
Dim screenY = GetScreenY()
TracePrint "分辨率: "&screenX&"*" &screenY
Log.Open 
TracePrint "当前设备的临时目录为：" &GetTempDir()
Dim auto_bool = ReadUIConfig("auto")

Call Screen()//屏幕适配 
Call main()

//主函数
Function main
    //自动进入界面
    If auto_bool = True Then 
        Call init()
    End If
    
    //成功进入
    Delay 2000
    //界面开始收取
    Call bumperHarvest()
    //搜集种物
    Call getSeed()
    //搜集阳光
    Call getSun()
    //偷阳光
    Call stealSun()
    //搜集阳光任务
    Call getSunTask()
    KeyPress "Home"

End Function
//初始函数
Function init
    Dim error_time =0
    TracePrint "进入到界面"
    //进入到界面
    While CmpColorEx("972|2044|FFFFFF",1) = 0
        TracePrint "正在进入到界面"
        RunApp "com.taobao.taobao"
        Delay 3000
        
        error_time = error_time + 1
        If error_time > 10 Then 
            TracePrint"出错"
            Exit Function
        End If
    Wend
    //进入我的淘宝
    TracePrint "进入我的淘宝"
    error_time =0
    Do
        Touch RndEx(970+5, 970+15), RndEx(2040+5, 2040+15), RndEx(30, 55)
        Delay 2000
        error_time = error_time + 1
        If error_time > 30 Then 
            TracePrint"出错"
            Exit Function
        End If
    Loop While CmpColorEx("970|2040|0074FE-111111",0.9) = 0

    //进入阳光牧场
    TracePrint "进入阳光农场"
    error_time =0
    Dim intX,intY
    Do
        FindColor 310,1885,416,1989,"78CD21-111111",0,0.9,intX,intY
        TracePrint "识别:x="&intX&"y="&intY
        If intX > -1 Then 
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            Delay RndEx(50, 100)
        End If
        Delay 5000
        error_time = error_time + 1
        If error_time > 30 Then 
            TracePrint"出错"
            Exit Function
        End If
    Loop While CmpColorEx("1017|138|26D16C", 1) = 0
    TracePrint "成功进入阳光农场"
	
End Function

Function bumperHarvest
    If CmpColorEx("409|1540|0372DF-111111",0.9) = 1 Then
        Touch RndEx(541+5, 541+15), RndEx(1687+5, 1687+15), RndEx(30, 55)
        Delay RndEx(500, 800)
    End If
	
	
	
End Function

//获取阳光
Function getSun
    TracePrint "获取阳光"
    Dim intX,intY
    Dim error_time =0
    Do
        FindColor 90,609,1031,1087,"4CCFFB",0,1,intX,intY
        If intX > -1 And intY > -1 Then 
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            TracePrint "识别:x="&intX&"y="&intY
        End If
        Delay RndEx(500, 1500)
        error_time = error_time + 1
        If error_time > 10 Then 
            TracePrint"出错"
            Exit Do
        End If
    Loop While intX>-1
	
	
End Function
//获取种物
Function getSeed
    TracePrint "获取种物"
    Dim x,y,intX,intY,i
    x = 288
    y = 1615
    For i = 1 To 7
        //第一块田
        FindColor x, y - 115, screenX/2, y- 100, "234387", 0, 1, intX, intY
        If intX > -1 And intY > -1 Then 
            TracePrint "识别:x="&intX&"y="&intY
            Touch RndEx(intX + 20, intX + 35), RndEx(intY + 10, intY - 10), RndEx(30, 55)
        End If
        Call receiveAbnormal()
        //第二块田
        If i Mod 2=0 Then
            FindColor screenX/2, y - 115, screenX/2 + 400, y- 100, "234387", 0, 1, intX, intY
            If intX > -1 And intY > -1 Then 
                TracePrint "识别:x="&intX&"y="&intY
                Touch RndEx(intX + 20, intX + 35), RndEx(intY + 10, intY - 10), RndEx(30, 55)
            End If
        End If
        Call receiveAbnormal()
        y = y - 120
        Delay RndEx(800, 1000)
    Next
End Function
//获取种物异常处理
Function receiveAbnormal
    Dim intX,intY
    //点击等级升级
    FindColor 403,1591,450,1646,"FFFFFF-111111",0,1,intX,intY
    If intX > -1 And intY > -1 Then
        TracePrint "点击等级升级:x="&intX&"y="&intY
        Touch RndEx(intX + 20, intX + 35), RndEx(intY + 10, intY - 10), RndEx(30, 55)
        Delay RndEx(500, 2000)
    End If
    //点击作物升级
    //    FindColor 528,1492,554,1509,"F5FFF5-111111",0,0.9,intX,intY
    //    If intX > -1 And intY > -1 Then
    //        TracePrint "点击作物升级:x="&intX&"y="&intY
    //        Touch RndEx(intX + 20, intX + 35), RndEx(intY + 10, intY - 10), RndEx(30, 55)
    //        Delay RndEx(500, 2000)     
    //    End If
    //解锁新作物
    If CmpColorEx("718|1485|4ABA5A", 1) = 1 And CmpColorEx("340|1476|313131",1) = 1 Then 
        TracePrint "解锁新作物"
        Touch RndEx(718 + 20, 718 + 35), RndEx(1485 + 10, 1485 - 10), RndEx(30, 55)
        Delay RndEx(500, 2000)     
    End If
	
End Function


//领阳光
Function getSunTask
    TracePrint "领阳光"
    Dim intX,intY,intX2,intY2
    FindColor 885,1645,1048,1816,"3F61FE-111111",0,1,intX,intY
    If intX > -1 And intY > -1 Then
        Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
    End If
    Delay 1000
    TracePrint "去浏览"
    For 4
        //去浏览
        FindColor 769,1277,1035,1452,"6E6E6E-111111",0,1,intX,intY
        If intX = -1 And intY = -1 Then 
            FindColor 850, 1277, 950, 1452, "F3F0FF", 0, 1, intX2, intY2
            If intX2 > -1 And intY2 > -1 Then 
                Touch RndEx(895 + 5, 895 + 15), RndEx(1329 + 5, 1329 + 15), RndEx(30, 55)
                Delay RndEx(3000 + 5, 4000 + 115)
                Call browse()
            End If
        End If
    Next
    TracePrint "去进店"
    For 4
        //去进店
        FindColor 850, 1655, 950, 1822, "F3F0FF", 0, 1, intX, intY
        If intX > -1 And intY > -1 Then 
            Touch RndEx(895 + 5, 895 + 15), RndEx(1715 + 5, 1715 + 15), RndEx(30, 55)
            Delay RndEx(4000 + 5, 5000 + 115)
            Call store()
        End If
    Next
End Function

//去浏览
Function browse
    Swipe screenX / 2, screenY - 500, screenX / 2, 50, RndEx(800, 1000)
    Delay RndEx(800 + 5, 1000 + 115)
    Swipe screenX / 2, screenY - 500, screenX / 2, 50, RndEx(800, 1000)
    Delay RndEx(900 + 5, 1000 + 115)
    Swipe screenX / 2, screenY - 500, screenX / 2, 50, RndEx(800, 1000)
    Delay RndEx(1000 + 5, 1000 + 115)
    Delay RndEx(11000 + 5, 13000 + 15)
    KeyPress "Back"
    Delay RndEx(1000 + 5, 1000 + 115)
End Function

//去进店
Function store
    //寻找宝箱
    Dim error_time =0
    Dim intX,intY,intX2,intY2
    Do
    
        FindColor 502, 580, 587, 1961, "3600FF", 0, 1, intX, intY
        //二次确定
        FindColor 502, 580, 587, 1961, "96CA62", 0, 1, intX2, intY2
        If intX > -1 And intX2 > -1 Then 
            TracePrint "找到宝箱"
            //找到宝箱
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            Delay RndEx(1000 + 5, 1000 + 115)
            Exit Do
        End If
        TracePrint "滑动"
        Swipe screenX / 2, screenY-200, screenX / 2, screenY/2, RndEx(500, 600)
        Delay RndEx(200, 400 )
        error_time = error_time + 1
        //找不到
        If error_time > 40 Then 
            TracePrint"出错"
            Exit Do
        End If
    Loop While intX = -1
    //确保返回了搜寻阳光界面
    Do
        KeyPress "Back"
        Delay 2000
        FindColor 827,1339,940,1763,"F3F0FF",0,0.9,intX,intY
    Loop While intX > -1
    TracePrint "已经返回搜寻阳光界面"
    Delay RndEx(1000 + 5, 1000 + 115)
End Function


//偷阳光
Function stealSun
    TracePrint "偷阳光"
    Dim intX,intY,intX2,intY2
    Dim error_time =0
    //正在打开偷阳光
    Call openStealSun()
		
    //无法领取
    If CmpColorEx("389|1228|3600FF-111111", 0.9) = 1 Then 
        Touch RndEx(389+5, 389+15), RndEx(1228+5, 1228+15), RndEx(30, 55)
        Delay RndEx(1000 + 5, 2000 + 115)
        Exit Function
    End If
    //遍历偷阳光
    Do
    	TracePrint "寻找阳光"
        FindColor 791,757,914,920, "5D9A33-111111", 0, 0.9, intX, intY
        If intX > -1 And intY > -1 Then
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            Delay RndEx(4000 + 5, 5000 + 115)
            //获取阳光
            Call getStealSun()
            KeyPress "Back"
            Delay RndEx(4000 + 5, 5000 + 115)
            //正在打开偷阳光
            Call openStealSun()
        End If
    Loop While intX > -1
    Touch RndEx(538+5, 538+15), RndEx(2005+5, 2005+15), RndEx(30, 55)
    Delay RndEx(1000 + 5, 2000 + 115)

End Function

//打开偷阳光
Function openStealSun
    TracePrint "打开偷阳光"
    Dim intX,intY,intX2,intY2
    Dim error_time =0
    //正在打开偷阳光
    Do
        FindColor 715,1691,815,1779,"06C5FC-111111",0,0.9,intX,intY
        If intX > -1 And intY > -1 Then
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            
        End If
        Delay RndEx(1000 + 5, 1000 + 115)
        
        //无法领取
        If CmpColorEx("389|1228|3600FF-111111", 0.9) = 1 Then 
            Exit Do
        End If

        //已经找到偷阳光
        FindColor 616,619,662,654,"28CAFF-111111",0,0.9,intX2,intY2
        error_time = error_time + 1
        //找不到
        If error_time > 5 Then 
            TracePrint"出错"
            Exit Do
        End If
    Loop While intX2 = -1

End Function

//获取阳光
Function getStealSun
    TracePrint "获取阳光"
    Dim intX,intY,intX2,intY2
    Dim error_time =0
    Do
        FindColor 90,609,1031,1087,"4CCFFB",0,1,intX,intY
        If intX > -1 And intY > -1 Then 
            Touch RndEx(intX+5, intX+15), RndEx(intY+5, intY+15), RndEx(30, 55)
            TracePrint "识别:x="&intX&"y="&intY
        End If
        Delay RndEx(500, 1500)
        
        //偷能量过多
        FindColor 670,997,719,1021,"184020",0,0.9,intX2,intY2
        If intX2 > -1 And intY2 > -1 Then
            TracePrint"偷能量过多"
            Exit Do
        End If
        error_time = error_time + 1
        
        If error_time > 10 Then 
            TracePrint"出错"
            Exit Do
        End If
    Loop While intX>-1
	
	TracePrint "获取阳光完成"
End Function


//适配分辨率
Function Screen
    Dim scrX,scrY
    //这里设置成开发的分辨率
    scrX = 1080
    scrY = 2160
    SetScreenScale scrX, scrY,1
    Dim src = scrX & scrY
End Function


//封装随机数函数
Function RndEx(min, max)
    //Int((最大值 - 最小值 + 1) * Rnd() + 最小值)
    RndEx = Int(((max-min) * Rnd()) + min)
End Function
Function OnScriptExit()
 
    ShowMessage "脚本已经停止！"
    Log.Close 
End Function

